package org.eyeTopLight.service.impl;

import org.eyeTopLight.dao.ClientDao;
import org.eyeTopLight.po.authEntity.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 进行Client的读取和验证
 */
@Service
public class MyClientDetailsService implements ClientDetailsService {
    public static final Logger logger = LoggerFactory.getLogger(MyClientDetailsService.class);

    @Resource
    private ClientDao clientDao;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

        Client searchClient = new Client();
        searchClient.setClient_id(clientId);

        Client client = null;
        try{
            client = clientDao.queryByPK(searchClient);
        }catch (Exception e){
            logger.error("查询数据库Client表操作失败",e);
            throw new RuntimeException("数据库操作失败");
        }
        if(client == null){
            throw new NoSuchClientException("客户不存在");
        }
        return client;
    }
}
